<template>
  <el-dialog title="角色分配" :visible="showRoleDialog" @close="btnCancel">
    <!-- 复选框 -->
    <!-- v-model双向绑定 roleIds -->
    <el-checkbox-group v-model="roleIds">
      <!-- 要循环的选项 -->
      <!-- 要显示角色名称  存储角色id  label是存储的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <!-- 按钮 -->
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { assignRoles } from '@/api/employees'
// 获取角色列表
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
export default {
  name: '',
  components: {},
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    // 用户的id
    userId: {
      type: String,
      default: null // 没有默认值
    }
  },
  data() {
    return {
      list: [], // 当前用户所有角色
      roleIds: [] // 当前用户有哪些角色
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRoleList()
  },
  mounted() {},
  methods: {
    // 获取用户所有角色
    async getRoleList() {
      const { rows } = await getRoleList({ page: 1, pagesize: 10 })
      // rows要循环的记录
      this.list = rows
    },
    // 获取当前用户的角色
    async getUserDetailById(id) {
      const { roleIds } = await getUserDetailById(id)
      // 解构出来  将用户所用的角色赋值给当前用户的对象
      this.roleIds = roleIds
    },
    async btnOk() {
      await assignRoles({ id: this.userId, roleIds: this.roleIds }) // 保存用户角色
      this.$emit('update:showRoleDialog', false)
      // this.showRoleDialog = false
    },
    btnCancel() {
      this.roleIds = [] // 取消的时候 重置
      this.$emit('update:showRoleDialog', false)
    }
  }
}
</script>
<style lang='scss' scoped>
</style>
